<?php

    if (!defined('EXPONENT')) exit('');

    $i18n = exponent_lang_loadFile('modules/loginmodule/actions/resetpass_send.php');

    if (!defined('SYS_USERS')) require_once(BASE.'subsystems/users.php');
    $u = exponent_users_getUserByName($_POST['username']);
    $return="";
    if ($u != null && $u->is_acting_admin == 0 && $u->is_admin == 0 && $u->email != '') {
        // 4/9/2009: immanuel192 block this line, just work with exponent mailing system
        //if (!defined('SYS_SMTP')) require_once(BASE.'subsystems/smtp.php');

        $tok = null;
        $tok->uid = $u->id;
        $tok->expires = time() + 2*3600;
        $tok->token = md5(time()).uniqid('');;

        $e_template = new template('loginmodule','_email_resetconfirm',$loc);
        $e_template->assign('token',$tok);
        $e_template->assign('user',$u);
        $msg = $e_template->render();

        //ADK - I had to hard code this link because the template->render() function was causing the '&' characters
        //      to come thru as '&amp' in the emails sent to the users making the URLs useless. 	
        //$msg .= "\r\n".URL_BASE.SCRIPT_RELATIVE . SCRIPT_FILENAME . "?module=loginmodule&action=resetpass_confirm&token=".$tok->token."&uid=".$tok->uid;

        require_once(BASE.'subsystems/mail.php');
        $mail = new exponentMail();
        $mail->addTo($u->email);
        $mail->subject($i18n['title']);
        $mail->addHTML($msg);
        $mail->addFrom(SMTP_FROMADDRESS,SITE_TITLE );
        // Send the message
        if ($mail->send() > 0) {
            $db->insertObject($tok,'passreset_token');
            $return=$i18n['sent'];
        } else{
            $return=$i18n['unable'];
        }
    } else {
        $return=$i18n['unable'];
    }
    
    // Oct 9, 2011- trungdt - add support ajax_request
    if (exponent_javascript_inAjaxAction())
    {
        $obj=null;
        $obj->status=1;
        if ($return == $i18n['unable'])
            $obj->status=0;
        $obj->text=$return;
        echo json_encode($obj);
        // break the loading operation
        exponent_theme_setFlagAjaxAction(true);
    }
    else
    {
        $template = new template('loginmodule','_final_message');
        $template->assign('message',$return);
        $template->output();
    }

?>
